package com.janyee.buddha.adapter

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Matrix
import android.util.Log
import android.util.TypedValue
import android.view.View
import android.view.ViewGroup
import com.janyee.buddha.R
import com.janyee.buddha.entity.PageEntity
import com.janyee.buddha.listener.BookViewListener
import com.janyee.buddha.util.ReaderUtil

import com.janyee.buddha.view.BookContent
import com.janyee.buddha.view.Painter
import java.util.*

/**
 * Created by kmlixh on 18/1/1.
 */

class BookFliperAdapter(val context: Context, private val builder: StringBuilder, var mLineSpacing: Float, var mPadding: Float, var index: Int, var mpainter: Painter) : IBookFliperAdapter, BookViewListener {


    private var startBmp: Bitmap
    private var endBmp: Bitmap
    var previous = BookContent(context)
    var current = BookContent(context)
    var next = BookContent(context)
    var pairList: ArrayList<PageEntity>
    var timer = Timer()

    init {
        pairList = ArrayList()
        pairList.add(PageEntity())
        pairList.add(PageEntity())
        pairList.add(PageEntity())
        current.setHeadText("current")
        previous.setHeadText("previous")
        next.setHeadText("next")
        var tempS: Bitmap? = BitmapFactory.decodeResource(context.resources, R.mipmap.arrow_start)
        var tempE: Bitmap? = BitmapFactory.decodeResource(context.resources, R.mipmap.arrow_end)
        var imgWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 25f, context.resources.displayMetrics)
        val scaleWidth = imgWidth / tempS!!.width
        val scaleHeight = imgWidth / tempS.height
        // 取得想要缩放的matrix参数
        val matrix = Matrix()
        matrix.postScale(scaleWidth, scaleHeight)
        startBmp = Bitmap.createBitmap(tempS, 0, 0, tempS.width, tempS.height, matrix, true)
        endBmp = Bitmap.createBitmap(tempE!!, 0, 0, tempE.width, tempE.height, matrix, true)
        tempE.recycle()
        tempE = null
        tempS.recycle()
        tempS = null

    }

    private lateinit var viewGroup: ViewGroup

    override fun init(viewGroup: ViewGroup) {
        this.viewGroup = viewGroup
        previous.bookViewer.setBookViewListener(this, 0)
        current.bookViewer.setBookViewListener(this, 1)
        next.bookViewer.setBookViewListener(this, 2)
        Log.e("====", "init bookview")

        viewGroup.addView(next.viewHolder)
        viewGroup.addView(current.viewHolder)
        viewGroup.addView(previous.viewHolder)
    }

    override operator fun hasNext(): Boolean {
        return pairList[2].count > 0
    }

    override fun hasPrevious(): Boolean {
        return pairList[0].count > 0
    }

    override operator fun next(): View {
        return next.viewHolder
    }

    override fun previous(): View {
        return previous.viewHolder
    }

    override fun current(): View {
        return current.viewHolder
    }

    override fun pageNext() {
        Log.e("====", "page next")
        pairList.removeAt(0)
        index = pairList.get(1).index
        current.bookViewer.postInvalidate()
        kotlin.run {
            pairList.add(ReaderUtil.generateLineTextFromIndex(mpainter, width.toFloat(), height.toFloat(), mLineSpacing, builder, pairList.get(1).nextIndex))
            next.bookViewer.postInvalidate()
            previous.bookViewer.postInvalidate()
        }
        Log.e("pairList", "" + pairList[0].index + "---" + pairList[1].index + "---" + pairList[2].index)
    }

    override fun pagePreviousPre() {
        var pageEntity = pairList.get(0)
        pairList.add(0, pageEntity)
        index = pairList.get(1).index
        current.bookViewer.postInvalidate()
        Log.e("pairList pre", "" + pairList[0].index + "---" + pairList[1].index + "---" + pairList[2].index)

    }

    override fun pagePrevious() {

        kotlin.run {
            pairList.removeAt(3)
            pairList.set(0, ReaderUtil.generateLineTextToIndex(mpainter, width.toFloat(), height.toFloat(), mLineSpacing, builder, index))
            previous.bookViewer.postInvalidate()
            next.bookViewer.postInvalidate()
        }
        Log.e("pairList pred", "" + pairList[0].index + "---" + pairList[1].index + "---" + pairList[2].index)
    }


    override fun callLongClick() {
        current.bookViewer.callLongClick()
    }

    override fun cancelLongClick() {
        current.bookViewer.cancelChoosed()
    }

    var width = 0
    var height = 0
    override fun onMeasureSize(width: Int, height: Int) {
        if (this.width != width && this.height != height) {
            this.width = width
            this.height = height
            Log.e("===", "measure size")
            timer.cancel()
            timer = Timer()
            var refreshTask = object : RefreshTask(width, height) {
                override fun run() {
                    pairList.set(0, ReaderUtil.generateLineTextToIndex(mpainter, width.toFloat(), height.toFloat(), mLineSpacing, builder, index))
                    pairList.set(1, ReaderUtil.generateLineTextFromIndex(mpainter, width.toFloat(), height.toFloat(), mLineSpacing, builder, index))
                    pairList.set(2, ReaderUtil.generateLineTextFromIndex(mpainter, width.toFloat(), height.toFloat(), mLineSpacing, builder, index + pairList.get(1).count))
                    next.bookViewer.postInvalidate()
                    current.bookViewer.postInvalidate()
                    previous.bookViewer.postInvalidate()
                }
            }
            timer.schedule(refreshTask, 20)
        }
    }


    override fun getLineInfo(type: Int): PageEntity {
        return pairList.get(type)
    }

    override fun getPainter(): Painter {
        return mpainter
    }
    override fun getText(): StringBuilder {
        return builder
    }

    override fun getStartBmp(): Bitmap {
        return startBmp
    }

    override fun getEndBmp(): Bitmap {
        return endBmp
    }

    override fun getLineSpacing(): Float {
        return mLineSpacing
    }

    override fun getPadding(): Float {
        return mPadding
    }
}
abstract class RefreshTask(val width:Int,val height: Int):TimerTask()
